package cn.iocoder.yudao.module.member.service.collectNotes;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.collectNotes.vo.*;
import cn.iocoder.yudao.module.member.convert.collectNotes.CollectNotesConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.collectNotes.CollectNotesDO;
import cn.iocoder.yudao.module.member.dal.mysql.collectNotes.CollectNotesMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import liquibase.pro.packaged.E;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * 收藏备注 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CollectNotesServiceImpl implements CollectNotesService {

    @Resource
    private CollectNotesMapper collectNotesMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCollectNotes(CollectNotesCreateReqVO createReqVO) {
        // 插入
        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        createReqVO.setUserId(loginUserId);
        CollectNotesDO collectNotes = CollectNotesConvert.INSTANCE.convert(createReqVO);
        collectNotes.setCreateTime(new Date());
        collectNotes.setCreator(WebFrameworkUtils.getLoginUserName());
        collectNotes.setUpdater(WebFrameworkUtils.getLoginUserName());
        if(StringUtils.isNotBlank(createReqVO.getRemarks())){
            collectNotes.setRemarks(createReqVO.getRemarks());
            collectNotesMapper.saveNotesRecord(collectNotes);
        }
        CollectNotesDO collectNotesDO = collectNotesMapper.selectOne(new QueryWrapper<CollectNotesDO>().eq("user_id", loginUserId)
                .eq("relation_id", createReqVO.getRelationId()).eq("relation_type", createReqVO.getRelationType()));
        if(null==collectNotesDO){
            collectNotesMapper.insert(collectNotes);
        }else{
            if(null!=createReqVO.getCollectStatus()){
                collectNotesDO.setCollectStatus(createReqVO.getCollectStatus());
            }
            if(StringUtils.isNotBlank(createReqVO.getRemarks())){
                collectNotesDO.setRemarks(createReqVO.getRemarks());
                collectNotesDO.setUpdateTime(new Date());
            }
            collectNotesMapper.updateById(collectNotesDO);
            return collectNotesDO.getId();
        }
        // 返回
        return collectNotes.getId();
    }



    @Override
    public void updateCollectNotes(CollectNotesUpdateReqVO updateReqVO) {
        // 更新
        CollectNotesDO updateObj = CollectNotesConvert.INSTANCE.convert(updateReqVO);
        collectNotesMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollectNotes(Long id) {
        // 删除
        collectNotesMapper.deleteById(id);
    }



    @Override
    public CollectNotesDO getCollectNotes(Long id) {
        return collectNotesMapper.selectById(id);
    }

    @Override
    public List<CollectNotesDO> getCollectNotesList(Collection<Long> ids) {
        return collectNotesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CollectNotesRespVO> getCollectNotesPage(CollectNotesPageReqVO pageReqVO) {
        // MyBatis Plus 查询
        IPage<CollectNotesRespVO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        // 转换返回
        Map<String, Object> params = new HashMap<>();
        if(null !=pageReqVO.getRelationType()){
            params.put("relationType",pageReqVO.getRelationType());
        }
        if(null !=pageReqVO.getRelationId()){
            params.put("relationId",pageReqVO.getRelationId());
        }
        mpPage.setRecords(collectNotesMapper.getCollectNotesPage(mpPage, params));
        return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
    }

    @Override
    public List<CollectNotesDO> getCollectNotesList(CollectNotesExportReqVO exportReqVO) {
        return collectNotesMapper.selectList(exportReqVO);
    }

}
